<?php                              
namespace app\index\controller;

use think\Controller;
use think\Db;
use demo\REST; 
use think\Cookie;
use think\Session;
use think\Request;
use phpmailer\phpmailer;


class Login extends Controller
{
     //加载denglu
    public function index()
    {
        $M = model('Post');
        $this->assign('title','发表帖子');
        $this->assign('channel',$M->getchannels());
        return $this->fetch('login');
    }

     //执行登录 
    public function dologin()
    {
        $name = $_POST['name'];
        $pwd = $_POST['pwd'];
        $code = $_POST['code'];
        // $status = $_POST['status'];
        // 验证数据
        // 判断验证码
        if ($code) {
            
        }


        // 根据账号查询信息
        $user = db('user')->where('name', $name)->whereor('tel', $name)->whereor('email', $name)->find();
        // 判断密码
        if ($pwd == $user['pwd'] ) {
            // 密码正确
            // 存数据


             // 将用户信息添加到 session
                Session::set('userinfo',$user);

                //获取用户角色
                $urole = Db::name('user_role')->where(['uid'=>['eq',$user['id']]])->select();
                // dump($urole);
                $rolelist = array();
                foreach( $urole as $v)
                {
                    $rolelist[] = $v['rid'];
                }
                $unode = Db::name('role_node')->where(['rid'=>['in',$rolelist]])->select();
                $nodelist = array();
                foreach($unode as $v){
                    $nodelist[] = $v['nid'];
                }
                // 查询指定用户的node表中的相关信息
                $usernode = Db::view('node',['mname','aname'])->where(['id'=>['in',$nodelist]])->select();
                //控制器名大写
                foreach ($usernode as $key=>$value){
                    $usernode[$key]['mname'] = ucfirst($value['mname']);
                }

                //$list 指定登录用户可用的 控制器=>方法
                $list = array();
                $list['Index'] = array('index');
                //遍历重新拼装
                foreach($usernode as $vo){
                    $list[$vo['mname']][] = $vo['aname'];
                    // 把修改和执行修改 添加和执行添加 拼装到一起
                    if($vo['aname']=="edit"){
                        $list[$vo['mname']][]="update";
                    }
                    if($vo['aname']=="create"){
                        $list[$vo['mname']][]="save";
                    }
                }
                // 将权限分配到session中
                Session::set('user.nodelist',$list);
                 $this->success('登录成功!','/index');
        }else{
                 // 密码错误
                $this->success('密码错误!','/index/login');
        }
    }

     /**
     * 退出系统处理
     * @return \think\response\View
     */
    public function out()
    {
        Session::clear();
        return $this->error('退出成功,返回登录页',url('/index'));
    }



        //加载注册
    public function zc()
    {
        return view();
    }
        //加载注册
    public function zhuce()
    {
        $M = model('Post');
        $this->assign('title','发表帖子');
        $this->assign('channel',$M->getchannels());
        return $this->fetch();
        
    }

    //执行注册
     public function doadd()
    {
       
        //接收post数据
        $data=input('post.');
        // 判断状态
        // 添加默认头像 
        $data['pic'] = 'moren.jpg';
        $list = db('user')->insert($data);
        $id = db('user')->getLastInsID();
        // 添加默认角色
        $rid['rid'] = 1;
        $rid['uid'] = $id;
        $role = db('user_role')->insert($rid);
        $lists = db('user')->select($id);
        if($lists){
        $this->success('注册成功', '/index/login');
        } else {
            $this->error('注册失败');
        }


    }
    



    //用户名验证
    public function username()
    {
        $pwd = $_GET['pwd'];
        $user = $_GET['user'];

        $name = Db::name('user')->where('U_Name',$user)->find();
        $pwd = Db::name('user')->where('U_Name',$pwd)->find();

        if(!empty($name)){
            if (!empty($pwd)) {
                return '登陆成功'; 
            }else{
                return '密码不正确';
            }    
        }else{
            return '此用户不存在';
        }
    }

    public function sendCode(Request $request)
    {
        $num = mt_rand(100000, 999999);
        $user = $_GET['user'];
        $tel = Db::name('user')->field('U_Tel')->where('U_Name',$user)->find();
        if(!empty($tel)) {
            $data = sendTemplateSMS('13917461357',array($num, '5'), "1");
            return json('发送成功');
        } else{
            return false;
        }    
    } 


    // 注册用户


    public function dozc(){
        //接收post数据
        $data=input('post.');
        // 判断状态
        if ( isset($data['status']) ) {
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $list = db('user')->insert($data);
        $id = db('user')->getLastInsID();
        // 添加默认角色
        $rid['rid'] = 1;
        $rid['uid'] = $id;
        $role = db('user_role')->insert($rid);
        
        // 还回页面
        $lists = db('user')->select($id);

        if($lists){
        //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
        $this->success('添加成功', '/admin/user');
        } else {
        //错误页面的默认跳转页面是返回前一页，通常不需要设置
            $this->error('添加失败');
        }
    }







        //邮箱接口
    public function email()
    {
        $vcode=rand(1000,9999);
        $shou = "1376850622@qq.com";
        $mail = new PHPMailer(); //建立邮件发送类
        $mail->CharSet = "UTF-8";
        $address ="1376850622@qq.com";
        $mail->IsSMTP(); // 使用SMTP方式发送
        $mail->Host = "smtp.qq.com"; // 您的企业邮局域名
        $mail->SMTPAuth = true; // 启用SMTP验证功能
        $mail->Username = "1376850622@qq.com"; // 邮局用户名(请填写完整的email地址)
        $mail->Password = "toyspomjxwbhfjdj"; // 邮局密码
        $mail->Port=25;
        $mail->From = "1376850622@qq.com"; //邮件发送者email地址
        $mail->FromName = "搞事情论坛";
        $mail->AddAddress($shou, "title");//收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
        $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
        $mail->Subject = "您的验证码是:"; //邮件标题
        $mail->Body = $vcode; //邮件内容，上面设置HTML，则可以是HTML

        if(!$mail->Send())
        {
           echo "邮件发送失败. <p>";
           exit;
        }else{
        return '发送成功';
        }
    }  
}
